

package ro.ase.catalog.security.action;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.context.SecurityContextHolder;
import ro.ase.catalog.beans.Role;
import ro.ase.catalog.beans.Setting;
import ro.ase.catalog.beans.Teacher;
import ro.ase.catalog.beans.User;
import ro.ase.catalog.security.service.ILoginService;
import ro.ase.catalog.security.service.ITeacherService;
import ro.ase.catalog.security.service.IUserService;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 *
 *
 * @see com.opensymphony.xwork2.ActionSupport
 */
public class LoginAction extends ActionSupport {
    private static final long serialVersionUID = -5277215719944190914L;

    @Autowired
    private ILoginService loginService;
    @Autowired
    private IUserService userService;
    private static final Logger logger = Logger.getLogger(LoginAction.class);
    private User user;
    private String node;
    private Set<Map<String, Object>> menu;
    private String pageSize;
    private String skin;
    @Autowired
    private ITeacherService teacherService;
    private Map<String, Object> loginResult = new HashMap<String, Object>();
    private Map<String, String> errorMsg = new HashMap<String, String>();
    private Map<String, Object> resultMap = new HashMap<String, Object>();

    public ITeacherService getTeacherService() {
        return teacherService;
    }

    public void setTeacherService(ITeacherService teacherService) {
        this.teacherService = teacherService;
    }

    public ILoginService getLoginService() {
        return loginService;
    }

    public void setLoginService(ILoginService loginService) {
        this.loginService = loginService;
    }

    public IUserService getUserService() {
        return userService;
    }

    public void setUserService(IUserService userService) {
        this.userService = userService;
    }

    public Map<String, Object> getResultMap() {
        return resultMap;
    }

    public void setResultMap(Map<String, Object> resultMap) {
        this.resultMap = resultMap;
    }


    // ~~~~~~~~~~~~~~~~~~~~~~~  Action Methods ~~~~~~~~~~~~~~~~~~~~~~~~~~//

    /**
     *
     *
     * @return "success"
     */
    public String loginSuccess() {
        loginResult.put("success", true);
        return SUCCESS;
    }

    /**
     *
     *
     * @return "success"
     */
    public String loginFailed() {
        logger.warn("a");
        errorMsg.put("reason", getText("error.login.authenticationFailed"));
        loginResult.put("success", false);
        loginResult.put("errorMsg", errorMsg);
        return SUCCESS;
    }

    /**
     *
     *
     * @return "success"
     */
    public String loadMenu() {

        User currentUser = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Teacher teacher=teacherService.findByUserName(currentUser.getLoginname());
        menu = loginService.loadChildNodes(currentUser, node, teacher);

        return SUCCESS;
    }


    /**
     *
     *
      * @return "success"
     */
    public String loadSetting() {
        User cUser = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Set<Setting> settings = userService.getSettings(cUser.getId());
        for (Setting setting : settings) {
            if ("RowsPerPage".equals(setting.getName())) {
                pageSize = setting.getValue();

            }
            if ("Skin".equals(setting.getName())) {
                skin = setting.getValue();
                logger.warn("af");
            }
        }

        return SUCCESS;
    }

    public String loadHomePage(){
        User logUser=  (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Iterator<Role> iterator = logUser.getRoles().iterator() ;
        while(iterator.hasNext()) {
            Role setElement = iterator.next();
            if(setElement.getName().equals("ROLE_ADMIN")) {
                loginResult.put("admin","admin");
            }
        }

        return SUCCESS;
    }

    // ~~~~~~~~~~~~~~~~~~~~~~~  Accessor Methods ~~~~~~~~~~~~~~~~~~~~~~~~~~//    

    /**
     * @return node
     */
    public String getNode() {
        return node;
    }

    /**
     * @param node
     */
    public void setNode(String node) {
        this.node = node;
    }

    /**
     * @return the user
     */
    public User getUser() {
        return user;
    }

    /**
     * @param user
     */
    public void setUser(User user) {
        this.user = user;
    }

    public Set<Map<String, Object>> getMenu() {
        return menu;
    }

    public void setMenu(Set<Map<String, Object>> menu) {
        this.menu = menu;
    }

    public Map<String, Object> getLoginResult() {
        return loginResult;
    }

    public void setLoginResult(Map<String, Object> loginResult) {
        this.loginResult = loginResult;
    }

    public Map<String, String> getErrorMsg() {
        return errorMsg;
    }

    public void setErrorMsg(Map<String, String> errorMsg) {
        this.errorMsg = errorMsg;
    }

    /**
     * @param pageSize the pageSize to set
     */
    public void setPageSize(String pageSize) {
        this.pageSize = pageSize;
    }

    /**
     * @return the pageSize
     */
    public String getPageSize() {
        return pageSize;
    }

    public void setSkin(String skin) {
        this.skin = skin;
    }


    public String getSkin() {
        return skin;
    }
}
